package net.wangds.auth.resource.comp;

import net.wangds.auth.module.ResourceSchemaEnum;
import net.wangds.auth.resource.entity.GeneralResourceEntity;
import net.wangds.auth.resource.repo.rdbms.ResourceJpaRepo;
import net.wangds.data.RemovedEnum;
import net.wangds.jdbc.comp.jdbc.JdbcComp;
import net.wangds.jpa.Sorts;
import net.wangds.jpa.comp.AbstractRemovedComp;
import net.wangds.jpa.repository.RemovableRepo;
import net.wangds.log.helper.LogHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class GeneralMenuComp extends AbstractRemovedComp<GeneralResourceEntity> {

    @Autowired
    private ResourceJpaRepo mmJpaRes;

    @Autowired
    private JdbcComp mmJdbc;

    protected GeneralMenuComp(RemovableRepo<GeneralResourceEntity> repo) {
        super(repo);
    }

    @Override
    public Page<GeneralResourceEntity> findByKeywords(String keywords, Pageable paging) {
        String sql = "SELECT t1.* \n" +
                "FROM auth_resource t1\n" +
                "where t1.protocol = 'MENU' and t1.removed = 0 and \n" +
                "( t1.resource_label regexp ? or t1.resource_desc regexp ? or t1.sys regexp ? or t1.module regexp ?)";
        LogHelper.dev("sql:"+sql+"\n"+keywords);
        return mmJdbc.executeQuery(sql, new Object[]{keywords, keywords, keywords,keywords}, paging, GeneralResourceEntity.class);
    }

    public List<GeneralResourceEntity> findMenus(String system) {

       return mmJpaRes.findBySchemaAndSysAndRemoved(ResourceSchemaEnum.MENU, system, RemovedEnum.NORMAL, Sorts.createTm_desc);

    }

    /**
     * 获得所有记录.
     * @param sort 排序.
     * @return 所有记录.
     */
    @SuppressWarnings("unused")
    public List<GeneralResourceEntity> findAll(Sort sort){
        return mmJpaRes.findBySchema(ResourceSchemaEnum.MENU, sort);
    }

    @Override
    public Page<GeneralResourceEntity> findAll(Pageable page) {
        return mmJpaRes.findBySchema(ResourceSchemaEnum.MENU, page);
    }
}
